package net.ecpt.clm.services;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;


public class ClmServices {

    public static String module = ClmServices.class.getName();

    //
    public static void main(String[] args) {
//    	String pattern="0000";
//    	java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
//    	int i = 10000;
//    	
//    	Long totalNum=new Long(i+1);
//    	System.out.println(df.format(totalNum));

	System.out.println(getCurrentYear());
    }

    public static String getDataDictionary(Delegator delegator, String dictionaryTypeId, String description)
	    throws GenericEntityException {
	String result = "";
	if (UtilValidate.isEmpty(description)) {
	    return result;
	}
	List<GenericValue> values = delegator.findByAnd("DataDictionary",
		UtilMisc.toMap("dictionaryTypeId", dictionaryTypeId, "description", description), null, true);
	GenericValue dataDictionary = EntityUtil.getFirst(values);
	if (UtilValidate.isNotEmpty(dataDictionary)) {
	    result = dataDictionary.getString("dictionaryCode");
	} else {
	    return description;
	}
	return result;
    }

    public static Map<String, Object> importPersonXlsToXml(DispatchContext ctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	try {
	    List<String> messageList = new ArrayList<String>();
	    List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");

	    String line = "\r\n";
	    StringBuffer sb = new StringBuffer();
	    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + line);
	    sb.append("<entity-engine-xml>" + line);
	    // Party PartyRole Person
	    // UserLogin UserLoginSecurityGroup
	    String temp_party = "<Party partyId=\"#1#\" partyTypeId=\"PERSON\" />";
	    String temp_partyRole = "<PartyRole partyId=\"#1#\" roleTypeId=\"EMPLOYEE\" />";
	    String temp_person = "<Person partyId=\"#1#\" firstName=\"#2#\" name=\"#2#\" nameEn=\"#3#\""
		    + " gender=\"#4#\" companyName=\"#5#\" departmentId=\"#6#\" idCard=\"#7#\" birthday=\"#8#\""
		    + " age=\"#9#\" politicId=\"#10#\" tiptopDegree=\"#11#\""
		    + " school=\"#12#\" specialty=\"#13#\" phone=\"#14#\" mobile=\"#14#\""
		    + " email=\"#15#\" address=\"#16#\" postCode=\"#17#\""
		    + " emergencyContact=\"#18#\" contactNumber=\"#19#\" agreementCode=\"#20#\""
		    + " beginDate=\"#21#\" positionRank=\"#22#\" positionName=\"#23#\""
		    + " reportTo=\"#24#\" beginContract=\"#25#\" workState=\"#26#\""
		    + " categoryId=\"#27#\" jobLevelId=\"#28#\" typeId=\"EMPLOYEE\"" + " wxUserId=\"#29#\"/>";

	    String temp_userLogin = "<UserLogin partyId=\"#1#\" userLoginId=\"#2#\" currentPassword=\"$SHA$G1mlbuiwVB9$QuqdELx__fBRDnN9wsgcjcv7SkA\" enabled=\"Y\" />";
	    String temp_userLoginSecurityGroup = "<UserLoginSecurityGroup userLoginId=\"#2#\" fromDate=\"2021-01-01 00:00:00\" groupId=\"8400\" />";

	    for (Map<String, Object> row : sheetList) {
		String name = ExcelPoiUtils.getCellString(row, 1);
		String nameEn = ExcelPoiUtils.getCellString(row, 2);
//		String personNum = ExcelPoiUtils.getCellString(row, 3);
		String genderDesc = ExcelPoiUtils.getCellString(row, 4);
		String gender = getDataDictionary(delegator, "USER_SEX", genderDesc);
		String companyName = ExcelPoiUtils.getCellString(row, 5);
		String departmentDesc = ExcelPoiUtils.getCellString(row, 6);
		String departmentId = "";
		List<GenericValue> departmentInfo = delegator.findByAnd("HrDepartment",
			UtilMisc.toMap("name", departmentDesc), null, false);
		if (UtilValidate.isNotEmpty(departmentInfo)) {
		    GenericValue department = EntityUtil.getFirst(departmentInfo);
		    departmentId = department.getString("departmentId");
		} else {
		    departmentId = departmentDesc;
		}

		String idCard = ExcelPoiUtils.getCellString(row, 7);
		Timestamp birthday = ExcelPoiUtils.getCellTimestamp2(row, 8);
		Long age = ExcelPoiUtils.getCellLong(row, 9);

		String politicIdDesc = ExcelPoiUtils.getCellString(row, 10);
		String politicId = getDataDictionary(delegator, "EMP_PARTY", politicIdDesc);

		String tiptopDegreeDesc = ExcelPoiUtils.getCellString(row, 11);
		String tiptopDegree = getDataDictionary(delegator, "EMP_EDUCATION", tiptopDegreeDesc);
		String school = ExcelPoiUtils.getCellString(row, 12);
		String specialty = ExcelPoiUtils.getCellString(row, 13);
		String mobile = ExcelPoiUtils.getCellString(row, 14);
		String wxUserId = ExcelPoiUtils.getCellString(row, 15);
		String email = ExcelPoiUtils.getCellString(row, 16);
		String address = ExcelPoiUtils.getCellString(row, 17);
		String postCode = ExcelPoiUtils.getCellString(row, 18);
		String emergencyContact = ExcelPoiUtils.getCellString(row, 19);
		String contactNumber = ExcelPoiUtils.getCellString(row, 20);
		String agreementCode = ExcelPoiUtils.getCellString(row, 21);
		Timestamp beginDate = ExcelPoiUtils.getCellTimestamp2(row, 22);
		String positionRankDesc = ExcelPoiUtils.getCellString(row, 23);
		String positionRank = getDataDictionary(delegator, "POSITION_RANK", positionRankDesc);
		String positionName = ExcelPoiUtils.getCellString(row, 24);
		String reportTo = ExcelPoiUtils.getCellString(row, 25);
		Timestamp beginContract = ExcelPoiUtils.getCellTimestamp2(row, 26);
		String workStateDesc = ExcelPoiUtils.getCellString(row, 27);
		String workState = getDataDictionary(delegator, "EMP_WORK_STATE", workStateDesc);
		String categoryIdDesc = ExcelPoiUtils.getCellString(row, 28); // 员工分类
		String categoryId = getDataDictionary(delegator, "STAFF_SORT", categoryIdDesc);
		String jobLevelId = ExcelPoiUtils.getCellString(row, 29);
//		String occupation = ExcelPoiUtils.getCellString(row, 30);

		String partyId = delegator.getNextSeqId("Party");

		String partyString = temp_party.replaceAll("#1#", partyId);
		String partyRoleString = temp_partyRole.replaceAll("#1#", partyId);

		String personString = temp_person.replaceAll("#1#", partyId);
		personString = personString.replaceAll("#2#", name);
		personString = personString.replaceAll("#3#", nameEn);
		personString = personString.replaceAll("#4#", gender);
		personString = personString.replaceAll("#5#", companyName);
		personString = personString.replaceAll("#6#", departmentId);
		personString = personString.replaceAll("#7#", idCard);
		if (UtilValidate.isNotEmpty(birthday)) {
		    personString = personString.replaceAll("#8#", birthday.toString());
		} else {
		    personString = personString.replaceAll("#8#", "");
		}
		if (UtilValidate.isNotEmpty(age)) {
		    personString = personString.replaceAll("#9#", age.toString());
		} else {
		    personString = personString.replaceAll("#9#", "");
		}
		personString = personString.replaceAll("#10#", politicId);
		personString = personString.replaceAll("#11#", tiptopDegree);
		personString = personString.replaceAll("#12#", school);
		personString = personString.replaceAll("#13#", specialty);
		personString = personString.replaceAll("#14#", mobile);
		personString = personString.replaceAll("#15#", email);
		personString = personString.replaceAll("#16#", address);
		personString = personString.replaceAll("#17#", postCode);
		personString = personString.replaceAll("#18#", emergencyContact);
		personString = personString.replaceAll("#19#", contactNumber);
		personString = personString.replaceAll("#20#", agreementCode);
		if (UtilValidate.isNotEmpty(beginDate)) {
		    personString = personString.replaceAll("#21#", beginDate.toString());
		} else {
		    personString = personString.replaceAll("#21#", "");
		}
		personString = personString.replaceAll("#22#", positionRank);
		personString = personString.replaceAll("#23#", positionName);
		personString = personString.replaceAll("#24#", reportTo);
		if (UtilValidate.isNotEmpty(beginContract)) {
		    personString = personString.replaceAll("#25#", beginContract.toString());
		} else {
		    personString = personString.replaceAll("#25#", "");
		}
		personString = personString.replaceAll("#26#", workState);
		personString = personString.replaceAll("#27#", categoryId);
		personString = personString.replaceAll("#28#", jobLevelId);
		personString = personString.replaceAll("#29#", wxUserId);

		String userString = temp_userLogin.replaceAll("#1#", partyId);
		userString = userString.replaceAll("#2#", wxUserId);

		String userSecString = temp_userLoginSecurityGroup.replaceAll("#2#", wxUserId);

		sb.append(line);
		sb.append(partyString);
		sb.append(line);
		sb.append(partyRoleString);
		sb.append(line);
		sb.append(personString);
		sb.append(line);
		if (UtilValidate.isNotEmpty(wxUserId)) {
		    sb.append(userString);
		    sb.append(line);
		    sb.append(userSecString);
		    sb.append(line + line);
		}
	    }

	    sb.append(line + "</entity-engine-xml>");
	    System.out.println(sb.toString());
	    String str = sb.toString();
	    str = str.replace("'", "");
	    String outPath = System.getProperty("ofbiz.home") + "/runtime/tmp/" + "person.xml";
	    PrintWriter pw = new PrintWriter(new FileWriter(outPath));
	    pw.write(str.toString());
	    pw.close();

	} catch (IOException e) {
	    Debug.log(e.getMessage());
	} catch (GenericEntityException e) {
	    e.printStackTrace();
	}
	return result;
    }

    public static Map<String, Object> importCompanyXlsToXml(DispatchContext ctx,
	    Map<String, ? extends Object> context) {
    	Delegator delegator = ctx.getDelegator();
	Map<String, Object> result = ServiceUtil.returnSuccess();
	try {
	    List<String> messageList = new ArrayList<String>();
	    List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	    String line = "\r\n";
	    StringBuffer sb = new StringBuffer();
	    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + line);
	    sb.append("<entity-engine-xml>" + line);
	    String temp_company = "<ClmCompany id=\"#1#\" companyCode=\"#2#\" typeId=\"#3#\""
		    + " isRelation=\"#4#\" companyName=\"#5#\" taxNumber=\"#6#\" description=\"#7#\" detailedAddress=\"#8#\""
		    + " isSyncNc=\"Y\" bankName=\"#10#\"/>";

	    for (Map<String, Object> row : sheetList) {
		String companyCode = ExcelPoiUtils.getCellString(row, 0);
		String companyName = ExcelPoiUtils.getCellString(row, 1);
		String taxNumber = ExcelPoiUtils.getCellString(row, 2);
		String relation = ExcelPoiUtils.getCellString(row, 3);
		String typeName = ExcelPoiUtils.getCellString(row, 4);
		String typeName2 = ExcelPoiUtils.getCellString(row, 5);
		String description = ExcelPoiUtils.getCellString(row, 6);
		String detailedAddress = ExcelPoiUtils.getCellString(row, 7);
		String bankName = ExcelPoiUtils.getCellString(row, 8);
		String typeId = "";
		String id = delegator.getNextSeqId("ClmCompany");
		if (typeName2.equals("客户")) {
		    if (typeName.equals("内部单位")) {
			typeId = "Internal";
		    } else {
			typeId = "Cusomer";
		    }
		} else if (typeName2.equals("供应商")) {
		    if (typeName.equals("内部单位")) {
			typeId = "Internal";
		    } else {
			typeId = "Supplier";
		    }
		}
		String isRelation = "N";
		if (relation.equals("关联方")) {
		    isRelation = "Y";
		    id = companyCode;
		}
		String personString = temp_company.replaceAll("#1#", id);
		personString = personString.replaceAll("#2#", companyCode);
		personString = personString.replaceAll("#3#", typeId);
		personString = personString.replaceAll("#4#", isRelation);
		personString = personString.replaceAll("#5#", companyName);
		personString = personString.replaceAll("#6#", taxNumber);
		personString = personString.replaceAll("#7#", description);
		personString = personString.replaceAll("#8#", detailedAddress);
		personString = personString.replaceAll("#10#", bankName);
		sb.append(line);
		sb.append(personString);
	    }

	    sb.append(line + "</entity-engine-xml>");
	    System.out.println(sb.toString());
	    String str = sb.toString();
	    str = str.replace("'", "");
	    String outPath = System.getProperty("ofbiz.home") + "/runtime/tmp/" + "person.xml";
	    PrintWriter pw = new PrintWriter(new FileWriter(outPath));
	    pw.write(str.toString());
	    pw.close();

	} catch (IOException e) {
	    Debug.log(e.getMessage());
	}
	return result;
    }

    public static Long getCurrentYear() {
	Date myDate = new Date();

	return new Long(1900 + myDate.getYear()); // 获取当前年份(2位)
    }

    public static Map<String, Object> getEmploymentAgreementCode(DispatchContext ctx,
	    Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	LocalDispatcher dispatcher = ctx.getDispatcher();

	String agreementCode = "";

	String fromCompanyId = (String) context.get("fromCompanyId");

	Long currentYear = getCurrentYear();
	// createYear

	try {
	    if (UtilValidate.isNotEmpty(fromCompanyId)) {

		GenericValue clmCompany = delegator.findOne("ClmCompany", UtilMisc.toMap("id", fromCompanyId), false);
		String companyCode = clmCompany.getString("emAgreementCode");

		List conditionList = new ArrayList();
		EntityCondition condition1 = EntityCondition.makeCondition("fromCompanyId", EntityOperator.EQUALS,
			fromCompanyId);
		conditionList.add(condition1);
		EntityCondition condition2 = EntityCondition.makeCondition("createYear", EntityOperator.EQUALS,
			currentYear);
		conditionList.add(condition2);

		Long totalNum = delegator.findCountByCondition("ClmAgreement",
			EntityCondition.makeCondition(conditionList), null, null);
		String pattern = "0000";
		java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
		agreementCode = companyCode + currentYear + df.format(totalNum + 1);
	    }

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	result.put("agreementCode", agreementCode);
	return result;
    }

    public static Map<String, Object> getPurchaseAgreementCode(DispatchContext ctx,
	    Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	LocalDispatcher dispatcher = ctx.getDispatcher();
	// GenericValue userLogin = (GenericValue) context.get("userLogin");

	String agreementCode = "";

	String fromCompanyId = (String) context.get("fromCompanyId");
	String specificTypeId = (String) context.get("specificTypeId");
	Long currentYear = getCurrentYear();

	try {
	    if (UtilValidate.isNotEmpty(fromCompanyId) && UtilValidate.isNotEmpty(specificTypeId)) {

		GenericValue clmCompany = delegator.findOne("ClmCompany", UtilMisc.toMap("id", fromCompanyId), false);
		String companyCode = clmCompany.getString("agreementCode");
		if (UtilValidate.isEmpty(companyCode)) {
		    companyCode = "";
		}

		List conditionList = new ArrayList();
		EntityCondition condition1 = EntityCondition.makeCondition("fromCompanyId", EntityOperator.EQUALS,
			fromCompanyId);
		EntityCondition condition2 = EntityCondition.makeCondition("specificTypeId", EntityOperator.EQUALS,
			specificTypeId);
		conditionList.add(condition1);
		conditionList.add(condition2);

		EntityCondition condition3 = EntityCondition.makeCondition("createYear", EntityOperator.EQUALS,
			currentYear);
		conditionList.add(condition3);
		Long totalNum = delegator.findCountByCondition("ClmAgreement",
			EntityCondition.makeCondition(conditionList), null, null);
		String pattern = "0000";
		java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
		agreementCode = specificTypeId + companyCode + currentYear + df.format(totalNum + 1);
	    }

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	result.put("agreementCode", agreementCode);
	return result;
    }

    public static Map<String, Object> getSaleAgreementCode(DispatchContext ctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	LocalDispatcher dispatcher = ctx.getDispatcher();
	// GenericValue userLogin = (GenericValue) context.get("userLogin");

	String agreementCode = "";

	String toCompanyId = (String) context.get("toCompanyId");
	String specificTypeId = (String) context.get("specificTypeId");
	Long currentYear = getCurrentYear();

	try {
	    if (UtilValidate.isNotEmpty(toCompanyId) && UtilValidate.isNotEmpty(specificTypeId)) {

		GenericValue clmCompany = delegator.findOne("ClmCompany", UtilMisc.toMap("id", toCompanyId), false);
		String companyCode = clmCompany.getString("saleAgreementCode");
		if (UtilValidate.isEmpty(companyCode)) {
		    companyCode = "";
		}
		List conditionList = new ArrayList();
		EntityCondition condition1 = EntityCondition.makeCondition("toCompanyId", EntityOperator.EQUALS,
			toCompanyId);
		EntityCondition condition2 = EntityCondition.makeCondition("specificTypeId", EntityOperator.EQUALS,
			specificTypeId);
		conditionList.add(condition1);
		conditionList.add(condition2);

		EntityCondition condition3 = EntityCondition.makeCondition("createYear", EntityOperator.EQUALS,
			currentYear);
		conditionList.add(condition3);

		Long totalNum = delegator.findCountByCondition("ClmAgreement",
			EntityCondition.makeCondition(conditionList), null, null);
		String pattern = "0000";
		java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
		agreementCode = specificTypeId + companyCode + currentYear + df.format(totalNum + 1);
	    }

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	result.put("agreementCode", agreementCode);
	return result;
    }

    public static Map<String, Object> getClmTemplateAndItem(DispatchContext ctx,
	    Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	LocalDispatcher dispatcher = ctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");

	String id = (String) context.get("id");

	try {
	    GenericValue clmTemplate = delegator.findOne("ClmTemplate", UtilMisc.toMap("id", id), false);

	    result.put("clmTemplate", clmTemplate);
	    List<GenericValue> clmTemplateItemList = delegator.findByAnd("ClmTemplateItem",
		    UtilMisc.toMap("templateId", id), UtilMisc.toList("sequenceNum"), true);
	    result.put("clmTemplateItemList", clmTemplateItemList);

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}

	return result;
    }

    /**
     * 
     * 小程序审批实体对象list
     * 
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Map<String, Object> listUniOaClmApproval(DispatchContext ctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	LocalDispatcher dispatcher = ctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");

	String createBy = userLogin.getString("partyId");

	try {
	    List nfsList = new ArrayList();

	    Map inputFields = new HashMap();
	    if (UtilValidate.isNotEmpty(context.get("inputFields"))) {
		inputFields = (Map) context.get("inputFields");
	    }
	    inputFields.put("createBy", createBy);

	    inputFields.put("noConditionFind", "Y");
	    Integer pageNo = 1;
	    if (UtilValidate.isNotEmpty(context.get("pageNo"))) {
		pageNo = (Integer) context.get("pageNo");
	    }
	    Integer pageSize = 20;
	    if (UtilValidate.isNotEmpty(context.get("pageSize"))) {
		pageSize = (Integer) context.get("pageSize");
	    }
	    String orderBy = "-createDate";
	    if (UtilValidate.isNotEmpty(context.get("orderBy"))) {
		orderBy = (String) context.get("orderBy");
	    }
	    String bizEntityName1 = (String) inputFields.get("bizEntityName");
	    List<String> bizEntityNameList = new ArrayList<>(Arrays.asList(bizEntityName1.split(",")));
	    inputFields.put("bizEntityName", bizEntityNameList);
	    Map serviceContext = new HashMap();

	    serviceContext.put("viewSize", pageSize);
	    serviceContext.put("viewIndex", pageNo - 1);

	    serviceContext.put("orderBy", orderBy);
	    serviceContext.put("entityName", "ClmApproval");
	    serviceContext.put("inputFields", inputFields);

	    Map serviceResult = dispatcher.runSync("performFindList", serviceContext);

	    List<GenericValue> gvList = (List<GenericValue>) serviceResult.get("list");

	    for (GenericValue gv : gvList) {
		Map map = new HashMap();
		String bizEntityName = gv.getString("bizEntityName");
		String bizEntityKey = gv.getString("bizEntityKey");
		if ("HrResign".equals(bizEntityName)) {
		    Map<String, Object> res = dispatcher.runSync("getHrResign",
			    UtilMisc.toMap("id", bizEntityKey, "userLogin", userLogin));
		    map.putAll((Map) res.get("nfs"));
		}
		if ("ClmCommonlySeal".equals(bizEntityName)) {
		    Map<String, Object> res = dispatcher.runSync("getClmCommonlySeal",
			    UtilMisc.toMap("id", bizEntityKey, "userLogin", userLogin));
		    map.putAll((Map) res.get("nfs"));
		}
		if ("ClmPurchaseRequest".equals(bizEntityName)) {
		    Map<String, Object> res = dispatcher.runSync("getClmPurchaseRequest",
			    UtilMisc.toMap("id", bizEntityKey, "userLogin", userLogin));
		    map.putAll((Map) res.get("nfs"));
		}
		if ("ClmPaymentOrder".equals(bizEntityName)) {
		    Map<String, Object> res = dispatcher.runSync("getClmPaymentOrder",
			    UtilMisc.toMap("id", bizEntityKey, "userLogin", userLogin));
		    map.putAll((Map) res.get("nfs"));
		}
//		GenericValue bizEntity = delegator.findOne(bizEntityName, UtilMisc.toMap("id", bizEntityKey), false);

//		map.putAll(bizEntity);
		map.putAll(gv);
		nfsList.add(map);
	    }
	    result.put("totalCount", serviceResult.get("listSize"));
	    result.put("nfsList", nfsList);

	} catch (GenericServiceException e) {
	    Debug.logError(e.getMessage(), module);

	}
//	catch (GenericEntityException e) {
//	    Debug.logError(e.getMessage(), module);
//	}

	return result;
    }

    public static Map<String, Object> selectClmTemplate(DispatchContext ctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();

	String typeId = (String) context.get("typeId");
	String specificTypeId = (String) context.get("specificTypeId");
	try {

	    Map param = UtilMisc.toMap("typeId", typeId);
	    if (UtilValidate.isNotEmpty(specificTypeId)) {
		param.put("specificTypeId", specificTypeId);
	    }

	    List<GenericValue> list = delegator.findByAnd("ClmTemplate", param, UtilMisc.toList("-createDate"), true);
	    result.put("nfsList", list);

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}

	return result;
    }

    public static Map<String, Object> selectDataDictionary(DispatchContext ctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
//		LocalDispatcher dispatcher = ctx.getDispatcher();
//		GenericValue userLogin = (GenericValue) context.get("userLogin");

	String dictionaryTypeId = (String) context.get("dictionaryTypeId");
	String parentId = (String) context.get("parentId");

	String parentCode = (String) context.get("parentCode");

	try {
	    if (UtilValidate.isNotEmpty(parentCode)) {
		List<GenericValue> parentDictionaryList = delegator.findByAnd("DataDictionary",
			UtilMisc.toMap("dictionaryTypeId", dictionaryTypeId, "dictionaryCode", parentCode),
			UtilMisc.toList("sequenceId"), true);
		GenericValue parentDictionary = EntityUtil.getFirst(parentDictionaryList);
		parentId = parentDictionary.getString("dictionaryId");
	    }

	    List<GenericValue> dictionaryList = delegator.findByAnd("DataDictionary",
		    UtilMisc.toMap("dictionaryTypeId", dictionaryTypeId, "parentId", parentId),
		    UtilMisc.toList("sequenceId"), true);
	    result.put("dictionaryList", dictionaryList);

	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}

	return result;
    }

//	public static Map<String, Object> setClmDataPass(DispatchContext ctx,
//		    Map<String, ? extends Object> context) {
//		Map<String, Object> result = ServiceUtil.returnSuccess();
//		Delegator delegator = ctx.getDelegator();
//		LocalDispatcher dispatcher = ctx.getDispatcher();
//		GenericValue userLogin = (GenericValue) context.get("userLogin");
//		List toBeStore =new ArrayList();
//
//		try {
//		    
//			
//			List<GenericValue> clmCompanyList = delegator.findByAnd("ClmCompany",UtilMisc.toMap("statusId", "0"), null, true);
//		    
//		    List<GenericValue> clmTemplateList = delegator.findByAnd("ClmTemplate",UtilMisc.toMap("statusId", "0"), null, true);
//		    
//		    List<GenericValue> clmAgreementList = delegator.findByAnd("ClmAgreement",UtilMisc.toMap("statusId", "0"), null, true);
//		    
//		    List<GenericValue> clmPaymentOrderList = delegator.findByAnd("ClmPaymentOrder",UtilMisc.toMap("statusId", "0"), null, true);
//		    
//		    
//		    
//		    
//		    
//		    
//		    
//		    for(GenericValue gv: clmCompanyList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    for(GenericValue gv: clmTemplateList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    for(GenericValue gv: clmAgreementList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    for(GenericValue gv: clmPaymentOrderList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    
//		    delegator.storeAll(toBeStore);
//		} catch (GenericEntityException e) {
//		    Debug.logError(e.getMessage(), module);
//		}
//
//		return result;
//	    }
//	
//	
//	public static Map<String, Object> setClmClmCompanyPass(DispatchContext ctx,
//		    Map<String, ? extends Object> context) {
//		Map<String, Object> result = ServiceUtil.returnSuccess();
//		Delegator delegator = ctx.getDelegator();
//		LocalDispatcher dispatcher = ctx.getDispatcher();
//		GenericValue userLogin = (GenericValue) context.get("userLogin");
//		List toBeStore =new ArrayList();
//
//		try {
//		    
//			
//			List<GenericValue> clmCompanyList = delegator.findByAnd("ClmCompany",UtilMisc.toMap("statusId", "1"), null, true);
//		    
//		    List<GenericValue> clmTemplateList = delegator.findByAnd("ClmTemplate",UtilMisc.toMap("statusId", "1"), null, true);
//		    
//		    List<GenericValue> clmAgreementList = delegator.findByAnd("ClmAgreement",UtilMisc.toMap("statusId", "1"), null, true);
//		    
//		    List<GenericValue> clmPaymentOrderList = delegator.findByAnd("ClmPaymentOrder",UtilMisc.toMap("statusId", "1"), null, true);
//		    for(GenericValue gv: clmCompanyList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    for(GenericValue gv: clmTemplateList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    for(GenericValue gv: clmAgreementList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    for(GenericValue gv: clmPaymentOrderList) {
//		    	gv.set("statusId", "2");
//		    	toBeStore.add(gv);
//		    }
//		    
//		    delegator.storeAll(toBeStore);
//		} catch (GenericEntityException e) {
//		    Debug.logError(e.getMessage(), module);
//		}
//
//		return result;
//	    }
    public static Map<String, Object> setClmCompanyPass(DispatchContext ctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	LocalDispatcher dispatcher = ctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	List toBeStore = new ArrayList();

	try {

	    List<GenericValue> clmCompanyList = delegator.findByAnd("ClmCompany", UtilMisc.toMap("statusId", "0"), null,
		    true);

	    for (GenericValue item : clmCompanyList) {

		GenericValue gv = delegator.findOne("ClmCompany", false, UtilMisc.toMap("id", item.getString("id")));
		gv.set("statusId", "2");
		toBeStore.add(gv);
	    }

	    delegator.storeAll(toBeStore);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}

	return result;
    }

    public static Map<String, Object> setClmTemplatePass(DispatchContext ctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = ctx.getDelegator();
	LocalDispatcher dispatcher = ctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	List toBeStore = new ArrayList();

	try {

	    List<GenericValue> clmTemplateList = delegator.findByAnd("ClmTemplate", UtilMisc.toMap("statusId", "0"),
		    null, true);

	    for (GenericValue item : clmTemplateList) {
		GenericValue gv = delegator.findOne("ClmTemplate", false, UtilMisc.toMap("id", item.getString("id")));
		gv.set("statusId", "2");
		toBeStore.add(gv);
	    }

	    delegator.storeAll(toBeStore);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}

	return result;
    }

}
